'use strict';

const Service = require('../core/common');

class GoodsService extends Service {
  findAll() {
    const { ctx } = this;

    const { goodsId } = ctx.params;

    // isWinning 已中奖的用户
    const { openId, nickname, isWinning, goodsPrizesId } = ctx.request.query;

    const where = {};
    const whereThrough = {}
    openId && Object.assign(where, { openId });
    nickname && Object.assign(where, { nickname: { '$like': `%${nickname}%` } });
    isWinning && Object.assign(whereThrough, { status: 1 });
    isWinning && goodsPrizesId && Object.assign(whereThrough, { goodsPrizesId });

    return ctx.model.Users.findAndCountAll({
      include: [{
        attributes: [],
        model: ctx.model.Goods,
        where: { id: goodsId },
        through: {
          attributes: [],
          where: { goodsId, ...whereThrough },
        },
      }],
      ...this.paging({ where }),
    });
  }
}

module.exports = GoodsService;